import QtQuick 1.1
import com.nokia.meego 1.0

import com.nokia.extras 1.1
import "WebRequest.js" as Web

PostPage {
    id: commentPost

    property Item prevPage

    headerText: "添加评论"
    placeHolderText: "这里不欢迎贴小广告、谩骂、色情、\n贩毒、卖军火等奇怪行为，\n您的言论将有可能作为禁言的呈堂证供。"
    onPost: {
        if (postText == "")
            Web.showMessage("请输入回复内容")
        else
            askDialog.open()
    }

    signal loadStart
    signal loadStop
    property bool loading: false
    onLoadStart: {appWindow.loadStart(); loading=true}
    onLoadStop: {appWindow.loadStop(); loading=false}
    Component.onDestruction: Web.xmlhttp.abort()

//     Dialog {
//          id: askDialog
//          content: CheckBox {
//              id: checkBox
//              width: parent.width
//              text: "匿名发送"
//          }
//          onButtonClicked: if(index==1)postComment()
//          onClickedOutside: close()
//      }

     MultiSelectionDialog {
         id: askDialog
         titleText: "确认操作"

         model: ListModel {
             ListElement { name: "匿名发送" }

         }
         acceptButtonText: "发送"
         rejectButtonText: "取消"
         onAccepted: postComment()
     }


    function postComment(){
        var param = { content: postText.replace(/\n/g, "\\n"), anonymous: askDialog.selectedIndexes===1 }
        Web.sendWebRequest("POST", "http://m2.qiushibaike.com/article/"+prevPage.postId+"/comment/create",
                           loadCommentResult, JSON.stringify(param))
    }
    function loadCommentResult(oritxt){
        var obj = JSON.parse(oritxt)
        if(obj.err === 0){
            Web.showMessage("评论发表成功！")
        } else {
            Web.showMessage(obj.err_msg)
        }
        prevPage.getCommentList(true)
        pageStack.pop()
    }
}
